/*
    Radio Battletoads Player
    Copyright (C) 2011 Jorge Suarez de Lis <yo@jorgesuarezdelis.name>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.radiobattletoads.player;

import java.util.Random;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.radiobattletoads.player.R;

public class NotificationManager {
	
	static Integer droppednotifid = null; 
	static Integer playingnotifid = null;
	
	public static void putPlayingNotification(Context c){
		if(playingnotifid==null){
			playingnotifid = NotificationManager.putNotification(c, R.drawable.icon, R.string.notifplaying,Notification.FLAG_ONGOING_EVENT);
		}
	}
	
	public static void putDroppedNotification(Context c){
		if(droppednotifid==null){
			droppednotifid = NotificationManager.putNotification(c, R.drawable.iconfault, R.string.notifdropped,Notification.FLAG_AUTO_CANCEL);
		}
	}
	
	private static Integer putNotification(Context c,int iconResource,int textResource,int flags){
		android.app.NotificationManager mn = (android.app.NotificationManager) c.getSystemService(Context.NOTIFICATION_SERVICE);
		Random r = new Random(System.currentTimeMillis());
        int notifid = r.nextInt(9999);
		Notification notif = new Notification(iconResource, c.getResources().getString(textResource) , System.currentTimeMillis());
		notif.flags = flags;
        Intent in = new Intent(c, RadioBattletoadsActivity.class);
        in.putExtra("playing_notification", notifid);
        in.setAction("android.intent.action.MAIN");
        PendingIntent contentIntent = PendingIntent.getActivity(c, 1, in, PendingIntent.FLAG_ONE_SHOT);
        notif.setLatestEventInfo(c, c.getResources().getString(R.string.app_name), c.getResources().getString(textResource), contentIntent);
        mn.notify(notifid, notif);
        return notifid;
	}
	
	public static void removePlayingNotification(Context c){
		if(playingnotifid!=null){
			android.app.NotificationManager mn = (android.app.NotificationManager) c.getSystemService(Context.NOTIFICATION_SERVICE);
			mn.cancel(playingnotifid);
			Log.d("notifications","Cancelling notification");
			playingnotifid = null;
		}
		Log.d("notifications","NOT Cancelling notification");
	}
	
	public static void removeDroppedNotification(Context c){
		if(droppednotifid!=null){
			android.app.NotificationManager mn = (android.app.NotificationManager) c.getSystemService(Context.NOTIFICATION_SERVICE);
			mn.cancel(droppednotifid);
			Log.d("notifications","Cancelling notification");
			droppednotifid = null;
		}
		Log.d("notifications","NOT Cancelling notification");
	}
	
	public static void removeAllNotifications(Context c){
		android.app.NotificationManager mn = (android.app.NotificationManager) c.getSystemService(Context.NOTIFICATION_SERVICE);
		mn.cancelAll();
		playingnotifid = null;
		droppednotifid = null;
	}

}